const User = require('../../model/user')
const { encrypt, decode } = require('../bcrypt')
const { create } = require('../jwt')
const { isFriend } = require('./friend')
//注册
var CreateUser = async (obj, res) => {
  try {
    var pwd = encrypt(obj.pwd)
    obj.pwd = pwd
    var data = await User.create(obj)
  } catch (error) {
    return res.send({
      status: 400
    })
  }
  res.send({
    status: 200
  })

}

//判断邮箱或用户名是否已经注册

var UserCount = async (obj, res) => {
  var data = await User.find(obj).count()
  if (data > 0) {
    res.send({
      status: 400
    })
  } else {
    res.send({
      status: 200
    })
  }
}

//判断是否注册
var isUser = async (data, pwd, res) => {
  // 接收请求参数
  let user = await User.findOne({ $or: [{ name: data }, { email: data }] }, { avatar: 1, pwd: 1, name: 1 })
  // console.log(user);
  // 查询到了用户
  if (user) {
    // 将客户端传递过来的密码和用户信息中的密码进行比对
    // true 比对成功
    // false 对比失败
    let isValid = decode(pwd, user.pwd);
    // 如果密码比对成功
    if (isValid) {
      // 生成token
      var token = create(user.user)
      res.send({
        token, status: 200, user: {
          name: user.name,
          avatar: user.avatar,
          id: user._id
        }
      })
    } else {
      // 密码错误
      res.send({
        status: 400
      })
    }
  } else {
    // 没有查询到用户
    res.send({
      status: 400
    })
  }
}

//搜索用户
var searchUser = async (key, id, res) => {
  var user = await User.find({
    $or: [{
      email: {
        $regex: key
      },
    }, {
      name: {
        $regex: key
      }
    }
    ], _id: {
      $ne: id
    }
  }).select({ name: 1, avatar: 1, email: 1 })
  var data =[]
  for(let i=0;i<user.length;i++){
    item=user[i]
    var a =await isFriend(id,item._id)
    if(a){
      data.push({id:item._id,avatar:item.avatar,name:item.name,isfriend:true,email:item.email})
    }else{
      data.push({id:item._id,avatar:item.avatar,name:item.name,isfriend:false,email:item.email})
    }
  }
  // console.log(data);
  res.send({
    status: 200,
    data
  })
}

var searchuserByid = async (id,res)=>{
  try {
    var data = await User.findOne({_id:id}).select('-_id -pwd ')
  } catch (error) {
    return res.send({
      status:400
    })
  }
  res.send({
    status:200,data
  })
}


var Edituser = async (id,obj,res)=>{
    try {
      await User.updateOne({_id:id},obj)
    } catch (error) {
      return res.send({
        status:400
      })
    }
    res.send({
      status:200
    })
}



var EditEmail =  async(id,pwd,email,res)=>{
  try {
    var user =  await User.findOne({_id:id})
    let isValid = decode(pwd, user.pwd)
    if(isValid){
      await User.updateOne({_id:id},{email:email})
    }else{
      return res.send({
        status:400,
        msg:'密码错误'
      })
    }
  } catch (error) {
    return res.send({
      status:400,
      msg:'请求失败'
    })
  }
  res.send({
    status:200
  })
}

var EditPwd =  async(id,opwd,npwd,res)=>{
  try {
    var user =  await User.findOne({_id:id})
    let isValid = decode(opwd, user.pwd)
    if(isValid){
      var pwd = encrypt(npwd)
      await User.updateOne({_id:id},{pwd})
    }else{
      return res.send({
        status:400,
        msg:'密码错误'
      })
    }
  } catch (error) {
    return res.send({
      status:400,
      msg:'请求失败'
    })
  }
  res.send({
    status:200
  })
}

module.exports = {
  CreateUser,
  UserCount,
  isUser,
  searchUser,
  searchuserByid,
  Edituser,
  EditEmail,
  EditPwd
}